package com.bsoft.gol.hcb.service;

import com.bsoft.gol.hcb.bean.request.GetPatientTagDTO;
import com.bsoft.gol.hcb.bean.request.SavePatientTagDTO;
import com.bsoft.gol.hcb.bean.response.PatientTagVO;
import com.bsoft.gol.hcb.dao.BasePatientPortraitDAO;
import com.bsoft.gol.hcb.dao.BasePatientTagDAO;
import ctd.account.UserRoleToken;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.KeyGenerator;
import ctd.util.annotation.RpcService;
import hcn.base.BasePatientPortrait;
import hcn.base.BasePatientTag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 患者标签服务
 *
 * @author neoLin - 2022/07/20 10:10
 **/
@SsdevService("patientTag")
public class PatientTagService {

    @Resource
    private BasePatientPortraitDAO basePatientPortraitDAO;

    @Resource
    private BasePatientTagDAO basePatientTagDAO;

    /**
     * 保存患者标签
     */
    @RpcService
    public void saveTag(SavePatientTagDTO dto) {
        String patientId = dto.getPatientId();
        String organizationId = dto.getOrganizationId();
        String tenantId = UserRoleToken.getCurrentTenantId();
        BasePatientPortrait query = new BasePatientPortrait();
        query.setPatientId(patientId);
        query.setOrganizationId(organizationId);
        query.setTenantId(tenantId);
        BasePatientPortrait exist = basePatientPortraitDAO.selectOne(query);
        BasePatientPortrait insertOrUpdate = new BasePatientPortrait();
        String remark = StringUtils.isBlank(dto.getRemark()) ? "" : dto.getRemark();
        insertOrUpdate.setPatientRemark(remark);
        insertOrUpdate.setPatientId(patientId);
        insertOrUpdate.setOrganizationId(organizationId);
        insertOrUpdate.setTenantId(tenantId);
        Date now = new Date();
        insertOrUpdate.setUpdateTime(now);
        String pId = null;
        if (Objects.nonNull(exist)) {
            pId = exist.getId();
            insertOrUpdate.setId(pId);
            basePatientPortraitDAO.updateSelective(insertOrUpdate);
        } else {
            pId = KeyGenerator.randomGenerate();
            insertOrUpdate.setId(pId);
            insertOrUpdate.setCreateTime(now);
            basePatientPortraitDAO.insertSelective(insertOrUpdate);
        }
        String fid = pId;
        List<BasePatientTag> insertList = new ArrayList<>();
        List<String> defaultTags = dto.getDefaultTags();
        if (!CollectionUtils.isEmpty(defaultTags)) {
            defaultTags.forEach(tag -> {
                BasePatientTag insert = new BasePatientTag();
                insert.setId(KeyGenerator.randomGenerate());
                insert.setPortraitId(fid);
                insert.setTenantId(tenantId);
                insert.setOrganizationId(organizationId);
                insert.setPatientId(patientId);
                insert.setPatientTag(tag);
                insert.setTagType("1");
                insert.setCreateTime(now);
                insert.setUpdateTime(now);
                insertList.add(insert);
            });
        }
        List<String> customTags = dto.getCustomTags();
        if (!CollectionUtils.isEmpty(customTags)) {
            customTags.forEach(tag -> {
                BasePatientTag insert = new BasePatientTag();
                insert.setId(KeyGenerator.randomGenerate());
                insert.setPortraitId(fid);
                insert.setTenantId(tenantId);
                insert.setOrganizationId(organizationId);
                insert.setPatientId(patientId);
                insert.setPatientTag(tag);
                insert.setTagType("2");
                insert.setCreateTime(now);
                insert.setUpdateTime(now);
                insertList.add(insert);
            });
        }

        basePatientTagDAO.deletePatientTag(patientId, organizationId, tenantId);
        if (!CollectionUtils.isEmpty(insertList)) {
            insertList.forEach(insert -> basePatientTagDAO.insertSelective(insert));
        }
    }

    /**
     * 获取患者标签
     *
     */
    @RpcService
    public PatientTagVO getTag(GetPatientTagDTO dto) {
        String patientId = dto.getPatientId();
        String organizationId = dto.getOrganizationId();
        String tenantId = UserRoleToken.getCurrentTenantId();

        BasePatientPortrait query = new BasePatientPortrait();
        query.setPatientId(patientId);
        query.setOrganizationId(organizationId);
        query.setTenantId(tenantId);
        BasePatientPortrait exist = basePatientPortraitDAO.selectOne(query);

        PatientTagVO vo = new PatientTagVO();
        if(Objects.isNull(exist)){
            return vo;
        }

        vo.setTenantId(exist.getTenantId());
        vo.setOrganizationId(exist.getOrganizationId());
        vo.setPatientId(exist.getPatientId());
        vo.setRemark(exist.getPatientRemark());

        BasePatientTag query2 = new BasePatientTag();
        query2.setPortraitId(exist.getId());
        List<BasePatientTag> basePatientTags = basePatientTagDAO.selectList(query2);

        List<String> defaultTags = new ArrayList<>();
        List<String> customTags = new ArrayList<>();

        if(!CollectionUtils.isEmpty(basePatientTags)){
            basePatientTags.forEach(tag->{
                String tagType = tag.getTagType();
                if("1".equals(tagType)){
                    defaultTags.add(tag.getPatientTag());
                }else if("2".equals(tagType)){
                    customTags.add(tag.getPatientTag());
                }
            });
        }
        vo.setDefaultTags(defaultTags);
        vo.setCustomTags(customTags);
        return vo;
    }
}
